package com.lequ.monitor.service;

import com.lequ.monitor.handler.SMSHandler;
import com.lequ.monitor.observer.SMSObserver;
import com.lequ.monitor.sms.SMS;


import android.os.Process;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class SMSService extends Service{

	    public static final String TAG = "SMSService";
	    
	    private ContentObserver mObserver;
	    private Handler mHandler = new Handler();
	 
	    @Override
	    public void onCreate(){
	       Log.i(TAG, "onCreate().");
	       addSMSObserver();
	       super.onCreate();   		    
	    }

	    public void addSMSObserver(){
	       Log.i(TAG, "add a SMS observer. ");
	       ContentResolver resolver = getContentResolver();
	       mHandler = new SMSHandler(this);
	       mObserver = new SMSObserver(resolver, mHandler);
	       resolver.registerContentObserver(SMS.CONTENT_URI, true, mObserver);
	    }

	    @Override
	    public IBinder onBind(Intent intent){
	       return null;
	    }

	    @Override
	    public void onDestroy(){
	       Log.i(TAG, "onDestroy().");
	      this.getContentResolver().unregisterContentObserver(mObserver);
	      super.onDestroy();
	       Process.killProcess(Process.myPid());	      
	       System.exit(0);
	    }

}
